package unq.edu.cui.tp.arena;

import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.bindings.NotNullObservable;
import org.uqbar.arena.layout.*;
import org.uqbar.arena.widgets.*;
import org.uqbar.arena.widgets.tables.Column;
import org.uqbar.arena.widgets.tables.Table;
import org.uqbar.arena.windows.*;

import unq.edu.cui.tp.dominio.Dia;
import unq.edu.cui.tp.modelo.ventanas.EditarEmpleado;

public class EditarEmpleadoWindow extends SimpleWindow<EditarEmpleado> {

	public EditarEmpleadoWindow(WindowOwner owner, EditarEmpleado model) {
		super(owner, model);
	}

	@Override
	protected void createFormPanel(Panel mainPanel) {
		
		this.setTitle("Empleado");
		this.setTaskDescription("");
		
		Panel form = new Panel(mainPanel);
		form.setLayout(new ColumnLayout(2));

		new Label(form).setText("Nombre");
		new TextBox(form).setWidth(100).bindValueToProperty("empleado.nombre");

		new Label(form).setText("Apellido");
		new TextBox(form).setWidth(100).bindValueToProperty("empleado.apellido");

		new Label(form).setText("Legajo");
		new TextBox(form).setWidth(100).bindValueToProperty("empleado.legajo");
		
		this.createGrid(mainPanel);
		this.createGridActions(mainPanel);

	}

	@Override
	protected void addActions(Panel actionsPanel) {
		
		new Button(actionsPanel)
		.setCaption("Cerrar")
		.onClick(new MessageSend(this, "close"));
	}
	
	protected void createGrid(Panel mainPanel) {
		
		Table<Dia> table = new Table<Dia>(mainPanel, Dia.class);
		table.setHeigth(200);
		table.setWidth(450);

		table.bindItemsToProperty("empleado.disponibilidad.diasDisponibles");
		table.bindValueToProperty("diaSeleccionado");

		this.describeGrid(table);
	}
	
	protected void describeGrid(Table<Dia> table) {
		new Column<Dia>(table)
			.setTitle("Dia")
			.setFixedSize(150)
			.bindContentsToProperty("nombreDia");

		new Column<Dia>(table)
			.setTitle("Desde")
			.setFixedSize(100)
			.bindContentsToProperty("desde");
		
		new Column<Dia>(table)
			.setTitle("Hasta")
			.setFixedSize(100)
			.bindContentsToProperty("hasta");
	}
	
	protected void createGridActions(Panel mainPanel) {
		Panel actionsPanel = new Panel(mainPanel);
		actionsPanel.setLayout(new HorizontalLayout());
		
		new Button(actionsPanel)
		.setCaption("Agregar")
		.onClick(new MessageSend(this, "agregarDisponibilidad"));
		
		Button remove = new Button(actionsPanel);
		remove.setCaption("Borrar");
		remove.onClick(new MessageSend(this.getModelObject(), "eliminarDiaSeleccionado"));
		
		// Deshabilitar el boton 'Borrar' si no hay ningún elemento seleccionado en la tabla.
		NotNullObservable elementSelected = new NotNullObservable("diaSeleccionado");
		remove.bindEnabled(elementSelected);
		
	}
	
	public void agregarDisponibilidad() {
		AgregarDisponibilidadWindow dialog = new AgregarDisponibilidadWindow(this, this.getModelObject().getEmpleado());
		dialog.onAccept(new MessageSend(this.getModelObject(), "cambiaronLasDisponibilidades"));
		this.openDialog(dialog);
	}
	
	protected void openDialog(Dialog<?> dialog) {
		dialog.open();
	}
	
	
	
}